package com.zy.socket.chat_0;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/19 18:58
 **/
public class ClientFrame  extends JFrame {
    private JPanel contentPane;
    private JLabel lblUserName;
    private JTextField tfMessage;
    private JButton btnSend;
    private JTextArea textArea;
    private String userName;
    private Client client;

    public ClientFrame(String ip,String userName){
        this.userName = userName;
        init();//添加窗体初始化内容
        addListener();
        client=new Client(ip,8080);
        ReadMessageThread t=new ReadMessageThread();
        t.start();
    }
    private class ReadMessageThread extends Thread{
        @Override
        public void run() {
            while(true) {
                String str=client.reciveMessage();
                textArea.append(str+"\n");//添加文本内容
            }
        }

    }
    private void init(){
        setTitle("客户端");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100,100,450,300);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        contentPane.setLayout(null);
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(5,5,434,229);
        contentPane.add(scrollPane);

        textArea =new JTextArea();
        scrollPane.setViewportView(textArea);
        textArea .setEditable(false);

        JPanel panel =new JPanel();
        panel.setBounds(5,235,434,32);
        contentPane.add(panel);
        panel.setLayout(null);

        lblUserName = new JLabel(userName);
        lblUserName.setHorizontalAlignment(SwingConstants.TRAILING);
        lblUserName.setBounds(2,4,55,22);
        panel.add(lblUserName);

        tfMessage =new JTextField();
        tfMessage.setBounds(62,5,274,22);
        tfMessage.setColumns(10);
        panel.add(tfMessage);

        btnSend =new JButton("发送");
        btnSend.setBounds(336,4,93,23);
        panel.add(btnSend);

        tfMessage.validate();
    }
    private void addListener(){
        btnSend.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                Date date=new Date();
                SimpleDateFormat sf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                client.sendMessage(userName+""+sf.format(date)+":\n"+tfMessage.getText());
                tfMessage.setText("");//清空

            }
        });
        this.addWindowListener(new WindowAdapter(){//开启窗口监听
            @Override
            public void windowClosing(WindowEvent atg0) {
                int op = JOptionPane.showConfirmDialog(ClientFrame.this,"确定要退出聊天室吗？","确定",JOptionPane.YES_NO_OPTION);
                if(op == JOptionPane.YES_OPTION) {
                    client.sendMessage("%EXIT%:" + userName);
                    try {
                        Thread.sleep(200);

                    }catch(InterruptedException e) {
                        e.printStackTrace();
                    }
                    client.close();
                    System.exit(0);
                }
            }
        });
    }
}